<?php
//This is a test controller that are to be removed at the final version of the system.
//This controller could be used to test functionality for handling users.
//Place this controller in the controller folder if you want to use it.
class C_user extends CI_Controller {

	/**
	 * Default function used to test if the controller is working.
	 *
	 * @param void
	 * @return void
	 */
	public function index()
	{
		echo 'Hello User-World!';
	}
	
	/**
	 * Function for creating the user.
	 *
	 * @param string username The username the user has entered.
	 * @param string password The password the user has entered.
	 * @return void
	 */
	public function create($username, $password)
	{
		//Creating an user object.
		$this->load->model('m_user', 'user');
		
		//Trying to create the user.
		$success = $this->user->create(array('username' => $username, 'password' => $password));
		
		//Printing the result.
		if ($success)
			echo "The user <i>$username</i> was created!";
		else
			echo "The user <i>$username</i> was NOT created!";
	}
	
	/**
	 * Function for changing a user password.
	 *
	 * @param string username The username the user has entered.
	 * @param string password The password the user has entered.
	 * @return void
	 */
	public function update()
	{
		//Loading the user model.
		$this->load->model('m_user');
		
		//Trying to load the user object from the session.
		$user = $this->session->userdata('user');
		
		//Dumping the user variable to see what we got.
		echo "<pre>";
		var_dump($user);
		echo "</pre>";
		
		//Trying to print the username in the same style.
		echo "<p>The username is " . $user->info['name'] . ".</p>";
	}
	
	/**
	 * Function to login the user.
	 *
	 * @param string username The username the user has entered.
	 * @param string password The password the user has entered.
	 * @return void
	 */
	public function login($username, $password)
	{
		//Creating an user object.
		$this->load->model('m_user', 'user');
		
		//Trying to login the user.
		$success = $this->user->login($username, $password);
		
		//Printing the result.
		if ($success) {
			//Creating a second user object (to see if it is possible to load object from CodeIgniter session).
			$user2 = $this->session->userdata('user');
			
			//Dumping the user variable to see what we got.
			echo "<pre>";
			var_dump($user2);
			echo "</pre>";
			
			//The following row successfully prints the name of the loged in user.
			echo "<p>The user " . $user2->info['name'] . " was loged in!</p>";
		}
		else
			echo "The user was NOT loged in!";
	}
}
?>